package org.millioncall.yueyoga.admin.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.CoachDAO;
import org.millioncall.yueyoga.admin.model.Coach;
import org.springframework.stereotype.Repository;

@Repository
public class CoachDAOImpl extends BaseDao implements CoachDAO {

	@Override
	public PaginatedList<Coach> findPage(int start, int limit) {
		try {
			PreparedQuery<Coach> pq = this.getPagableSqlSessionTemplate()
					.findAll("findCoachPage");
			PaginatedList<Coach> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));
			return pl;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public void deleteByIds(Integer[] deids) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < deids.length; i++) {
			map.put("id", deids[i]);
			this.getPagableSqlSessionTemplate().insert("deleteCoach", map);
		}
	}

	@Override
	public int save(Map<String, Object> map) {
		Date now = new Date();
		map.put("createTime", now);
		map.put("lastModifyTime", now);
		return this.getPagableSqlSessionTemplate().insert("saveCocah", map);
	}

	@Override
	public int update(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("updateCocah", map);
	}

	@Override
	public PaginatedList<Coach> findPage(int start, int limit,
			Map<String, Object> map) {
		PreparedQuery<Coach> pq = this.getPagableSqlSessionTemplate().findAll(
				"findCoachPages", map);
		PaginatedList<Coach> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public List<Coach> findAll(Map<String, Object> map) {
		PreparedQuery<Coach> pq = this.getPagableSqlSessionTemplate().findAll(
				"findCoachPage", map);
		List<Coach> list = pq.list();
		return list;
	}

	@Override
	public Coach findById(Integer id) {
		return this.getPagableSqlSessionTemplate().selectOne("findCoachById",
				id);
	}

	@Override
	public List<Coach> findCoach0() {
		PreparedQuery<Coach> pq = this.getPagableSqlSessionTemplate().findAll(
				"findCoach0");
		List<Coach> list = pq.list();
		return list;
	}

	@Override
	public List<Coach> findCoach1() {
		PreparedQuery<Coach> pq = this.getPagableSqlSessionTemplate().findAll(
				"findCoach1");
		List<Coach> list = pq.list();
		return list;
	}

	@Override
	public List<Coach> findAllTeacherByOrganizationId(Map<String, Object> map) {
		PreparedQuery<Coach> pq = this.getPagableSqlSessionTemplate().findAll("findAllTeacherByOrganizationId", map);
		return pq.list();
	}

	@Override
	public List<Coach> findAllAssistantByOrganizationId(Map<String, Object> map) {
		PreparedQuery<Coach> pq = this.getPagableSqlSessionTemplate().findAll("findAllAssistantByOrganizationId", map);
		return pq.list();
	}
}
